home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS 1.31 / h / DLexerBase.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-10  |  5.0 KB  |  209 lines  |  [TEXT/MPS ]

  1. /* DLGLexerBase.c
  2.  *
  3.  * SOFTWARE RIGHTS
  4.  *
  5.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  6.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  7.  * company may do whatever they wish with source code distributed with
  8.  * PCCTS or the code generated by PCCTS, including the incorporation of
  9.  * PCCTS, or its output, into commerical software.
  10.  * 
  11.  * We encourage users to develop software with PCCTS.  However, we do ask
  12.  * that credit is given to us for developing PCCTS.  By "credit",
  13.  * we mean that if you incorporate our source code into one of your
  14.  * programs (commercial product, research project, or otherwise) that you
  15.  * acknowledge this fact somewhere in the documentation, research report,
  16.  * etc...  If you like PCCTS and have developed a nice tool with the
  17.  * output, please mention that you developed it using PCCTS.  In
  18.  * addition, we ask that this header remain intact in our source code.
  19.  * As long as these guidelines are kept, we expect to continue enhancing
  20.  * this system and expect to make other tools available as they are
  21.  * completed.
  22.  *
  23.  * ANTLR 1.31
  24.  * Terence Parr
  25.  * Parr Research Corporation
  26.  * with Purdue University and AHPCRC, University of Minnesota
  27.  * 1989-1995
  28.  */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32. /* I have to put this here due to C++ limitation
  33.  * that you can't have a 'forward' decl for enums.
  34.  * I hate C++!!!!!!!!!!!!!!!
  35.  */
  36. enum TokenType { TER_HATES_CPP, ITS_UTTER_GARBAGE, WITH_SOME_GOOD_IDEAS };
  37.  
  38. #include "config.h"
  39. #include DLEXERBASE_H
  40.  
  41. DLGLexerBase::
  42. DLGLexerBase(DLGInputStream *in, unsigned bufsize, int interactive, int track_columns)
  43. {
  44.     this->_bufsize = bufsize;
  45.     this->_lextext = new DLGChar[_bufsize];
  46.     if ( this->_lextext==NULL ) {
  47.         /* FATAL: what to do? */
  48.     }
  49.     this->_begexpr = this->_endexpr = NULL;
  50.     this->ch = this->bufovf = 0;
  51.     this->nextpos = NULL;
  52.     this->cl = 0;
  53.     this->add_erase = 0;
  54.     this->input = in;
  55.     this->_begcol = 0;
  56.     this->_endcol = 0;
  57.     this->_line = 1;
  58.     this->charfull = 0;
  59.     this->automaton = 0;
  60.     this->token_to_fill = NULL;
  61.     this->interactive = interactive;
  62.     this->track_columns = track_columns;
  63. }
  64.  
  65. void DLGLexerBase::
  66. setInputStream( DLGInputStream *in )
  67. {
  68.     this->input = in;
  69.     _line = 1;
  70.     charfull = 0;
  71. }
  72.  
  73. /* saves dlg state, but not what feeds dlg (such as file position) */
  74. void DLGLexerBase::
  75. saveState(DLGState *state)
  76. {
  77.     state->input = input;
  78.     state->interactive = interactive;
  79.     state->track_columns = track_columns;
  80.     state->auto_num = automaton;
  81.     state->add_erase = add_erase;
  82.     state->lookc = ch;
  83.     state->char_full = charfull;
  84.     state->begcol = _begcol;
  85.     state->endcol = _endcol;
  86.     state->line = _line;
  87.     state->lextext = _lextext;
  88.     state->begexpr = _begexpr;
  89.     state->endexpr = _endexpr;
  90.     state->bufsize = _bufsize;
  91.     state->bufovf = bufovf;
  92.     state->nextpos = nextpos;
  93.     state->class_num = cl;
  94. }
  95.  
  96. void DLGLexerBase::
  97. restoreState(DLGState *state)
  98. {
  99.     input = state->input;
  100.     interactive = state->interactive;
  101.     track_columns = state->track_columns;
  102.     automaton = state->auto_num;
  103.     add_erase = state->add_erase;
  104.     ch = state->lookc;
  105.     charfull = state->char_full;
  106.     _begcol = state->begcol;
  107.     _endcol = state->endcol;
  108.     _line = state->line;
  109.     _lextext = state->lextext;
  110.     _begexpr = state->begexpr;
  111.     _endexpr = state->endexpr;
  112.     _bufsize = state->bufsize;
  113.     bufovf = state->bufovf;
  114.     nextpos = state->nextpos;
  115.     cl = state->class_num;
  116. }
  117.  
  118. /* erase what is currently in the buffer, and get a new reg. expr */
  119. void DLGLexerBase::
  120. skip()
  121. {
  122.     add_erase = 1;
  123. }
  124.  
  125. /* don't erase what is in the lextext buffer, add on to it */
  126. void DLGLexerBase::
  127. more()
  128. {
  129.     add_erase = 2;
  130. }
  131.  
  132. /* substitute c for the reg. expr last matched and is in the buffer */
  133. void DLGLexerBase::
  134. replchar(DLGChar c)
  135. {
  136.     /* can't allow overwriting null at end of string */
  137.     if (_begexpr < &_lextext[_bufsize-1]){
  138.         *_begexpr = c;
  139.         *(_begexpr+1) = '\0';
  140.     }
  141.     _endexpr = _begexpr;
  142.     nextpos = _begexpr + 1;
  143. }
  144.  
  145. /* replace the string s for the reg. expr last matched and in the buffer */
  146. void DLGLexerBase::
  147. replstr(register DLGChar *s)
  148. {
  149.     register DLGChar *l= &_lextext[_bufsize -1];
  150.  
  151.     nextpos = _begexpr;
  152.     if (s){
  153.          while ((nextpos <= l) && (*(nextpos++) = *(s++))){
  154.             /* empty */
  155.         }
  156.         /* correct for NULL at end of string */
  157.         nextpos--;
  158.     }
  159.     if ((nextpos <= l) && (*(--s) == 0)){
  160.         bufovf = 0;
  161.     }else{
  162.         bufovf = 1;
  163.     }
  164.     *(nextpos) = '\0';
  165.     _endexpr = nextpos - 1;
  166. }
  167.  
  168. void DLGLexerBase::
  169. errstd(char *s)
  170. {
  171.         fprintf(stderr,
  172.                 "%s near line %d (text was '%s')\n",
  173.                 ((s == NULL) ? "Lexical error" : s),
  174.                 _line,_lextext);
  175. }
  176.  
  177. int DLGLexerBase::
  178. err_in()
  179. {
  180.     fprintf(stderr,"No input stream, function, or string\n");
  181.     /* return eof to get out gracefully */
  182.     return EOF;
  183. }
  184.  
  185. TokenType DLGLexerBase::
  186. erraction()
  187. {
  188.     errstd("invalid token");
  189.     advance();
  190.     skip();
  191.     return (TokenType) 0;    // bogus, but satisfies compiler
  192. }
  193.  
  194. ANTLRAbstractToken *DLGLexerBase::
  195. getToken()
  196. {
  197.     if ( token_to_fill==NULL ) panic("NULL token_to_fill");
  198.     TokenType tt = nextTokenType();
  199.     DLGBasedToken *tk = (DLGBasedToken *)token_to_fill->makeToken(tt, _lextext,_line);
  200.     return tk;
  201. }
  202.  
  203. void DLGLexerBase::
  204. panic(char *msg)
  205. {
  206.     fprintf(stderr, "DLG panic: %s\n", msg);
  207.     exit(-1);
  208. }
  209.